﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class test : MonoBehaviour
{
    // Start is called before the first frame update
    private Text text;
    private Image img;
    void Start()
    {	
    	text = GameObject.Find("Text").GetComponent<Text>();
        img = GameObject.Find("Image").GetComponent<Image>();
        img.transform.gameObject.SetActive(false);
    	//使用www加载文本
        // StartCoroutine(LoadText());
        //使用www图片并赋值给Image的sprite
        StartCoroutine(LoadImage());
    }
    
	IEnumerator LoadText()
    {
    	string url = "http://192.168.102.16:8080/myConfig/1.0.1/notice.txt";
	    WWW www = new WWW(url);
	    yield return www; //等待下载完成
	    if (www.error == null)
        { 
            text.text = www.text;
        }
        else
        {
            text.text = "GET请求出错";
        }
    }	

    IEnumerator LoadImage()
    {
    	string url = "http://192.168.102.16:8080/myConfig/unity.jpeg";
	    WWW www = new WWW(url);
	    yield return www; //等待下载完成
	    if (www.error == null)
        { 
        	Texture2D tex = new Texture2D(www.texture.width, www.texture.height);
			//加载图像到纹理
	        www.LoadImageIntoTexture(tex);
	        img.sprite = Sprite.Create(tex, new Rect(0, 0, www.texture.width, www.texture.height), Vector2.zero);
	        img.SetNativeSize();
	        img.transform.gameObject.SetActive(true);
        }
        else
        {
            text.text = "GET图片请求出错";
        }
    }
}
